#ifndef _AV_ENCODE_H_
#define _AV_ENCODE_H_

#include "DVR_Enc.h"
#include "TGCriticalSection.h"

struct H264SPSPPS
{
	int   nSpsSize;
	int   nPpsSize;
	BYTE  Sps[32];
	BYTE  Pps[32];
	H264SPSPPS()
	{
		memset(this,0,sizeof(*this));
	}
};

class CAVEncode
{
public:

	HANDLE m_hSdkHeap;
	void * sdkMalloc(unsigned len);
	void   sdkFree(void * p);

	CAVEncode(void);
	TGCriticalSection m_xEncoder;

public:
	TGERR_CODE Init(int nSrcWidth,int nSrcHeight,DVR_PAR264* par,ChatEncodeCallBack funCallBack,DWORD dwContext,void* pContext);
	void		 Release();
	TGERR_CODE SetPar264(DVR_PAR264* par);
	TGERR_CODE Audio(DWORD lTimestamp,BYTE* pSrcData,long lSrcLen);
	TGERR_CODE Video(DWORD lTimestamp,unsigned char *pYUV,int ySize,int uSize,int vSize);
	void		 RegCallback(ChatEncodeCallBack funCallBack,DWORD dwContext,void *pContext);
	void		 BuildFrame(H264SPSPPS *pSpsPps,unsigned short nFrameNum,DWORD lTimestamp,uint8_t *pNalData,int nNalSize,BYTE *pFrame,DWORD &dwFrameSize);
	void		 BuildFrameTG(H264SPSPPS *pSpsPps,unsigned short nFrameNum,DWORD lTimestamp,uint8_t *pNalData,int nNalSize,BYTE *pFrame,DWORD &dwFrameSize);
private:
	x264_t *			m_pEncoder;
	x264_picture_t		m_xPic264;
	x264_param_t		m_xPar264;
	x264_picture_t		m_xPic_out;	
 	uint8_t *			m_pMuxBuf;
	H264SPSPPS			m_xSPSPPS;
  	DWORD				m_dwMaxBufferSize;
	BYTE *				m_pFrameBuffer;
	ChatEncodeCallBack	CB_ChatEncode ;
	DWORD               m_dwContext  ;
	void  *				m_pCBcontext;

	unsigned short      m_nFrameNum ;
	BYTE *				m_pFrameBufferTG;
	DWORD				m_dwMaxBufferSizeTG;
};

#endif